package com.example.guiguiapplicatio.fragment.examination;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.guiguiapplicatio.api.GetExaminationSigningApi;
import com.example.guiguiapplicatio.api.Interface.GetExaminationSigningCallback;
import com.example.guiguiapplicatio.api.Interface.SearchExaminationSigningCallback;
import com.example.guiguiapplicatio.api.SearchExaminationSigningApi;
import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.Response.ExaminationSigning;
import com.example.guiguiapplicatio.databinding.FragmentExaminationStartBinding;
import com.example.guiguiapplicatio.fragment.examination.adapter.ExaminationStartAdapter;
import com.example.guiguiapplicatio.fragment.examination.placeholder.ExaminationSigningPlaceholderContent;
import com.example.guiguiapplicatio.fragment.examination.placeholder.ExaminationSigningPlaceholderContent.PlaceholderItem;
import com.example.guiguiapplicatio.util.ResponseBody;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;


public class ExaminationStartFragment extends Fragment implements GetExaminationSigningCallback, SearchExaminationSigningCallback {
    FragmentExaminationStartBinding binding;
    ExaminationStartAdapter adapter;
    public static List<PlaceholderItem> NOW_ITEMS = new ArrayList<PlaceholderItem>();
    private GetExaminationSigningApi getExaminationSigningApi;
    private SearchExaminationSigningApi searchExaminationSigningApi;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentExaminationStartBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        binding.examinationStartList.setLayoutManager(new LinearLayoutManager(binding.getRoot().getContext()));
        NOW_ITEMS = ExaminationSigningPlaceholderContent.ITEMS;
        adapter = new ExaminationStartAdapter(NOW_ITEMS, getContext());
        binding.examinationStartList.setAdapter(adapter);

        getExaminationSigningApi = new GetExaminationSigningApi(this);
        getExaminationSigningApi.setGetExaminationSigningCallback(this);
        getExaminationSigningApi.performGetExaminationSigning();

        searchExaminationSigningApi=new SearchExaminationSigningApi(this);
        searchExaminationSigningApi.setSearchExaminationSigningCallback(this);
        binding.searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String idcardname=binding.etSearchtext.getText().toString();
                if(!idcardname.isEmpty()){
                    Log.d("info",idcardname);
                    searchExaminationSigningApi.performSearchExaminationSigning(idcardname);
                }else {
                    getExaminationSigningApi.performGetExaminationSigning();
                }
                binding.etSearchtext.setText("");
            }
        });
        binding.servicBack.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_examinationStartFragment_to_examinationRecordFragment));
        return view;
    }

    @Override
    public void onResume() {
        BottomNavigationView bottomNavigationView = requireActivity().findViewById(R.id.nav_view);
        bottomNavigationView.setVisibility(View.INVISIBLE);
        //这两行代码设置隐藏底部导航栏，若要显示啧修改INVISIBLE为Visible（可见）
        super.onResume();
    }

    @Override
    public void onESSuccess(ResponseBody<List<ExaminationSigning>> responseBody) {
        Object data = responseBody.getData();
        if (data instanceof List) {
            List<ExaminationSigning> examinationSignings = (List<ExaminationSigning>) data;
            NOW_ITEMS.clear();
            for (ExaminationSigning examinationSigning : examinationSignings) {
                ExaminationSigningPlaceholderContent.addItem(new ExaminationSigningPlaceholderContent.PlaceholderItem(
                        examinationSigning.getIdcard(),
                        examinationSigning.getName(),
                        examinationSigning.getSex(),
                        examinationSigning.getAge(),
                        examinationSigning.getListenername()
                ));
            }
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onESFailure(String errorMessage) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), "体检签约列表响应失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onSearchESSuccess(ResponseBody<List<ExaminationSigning>> responseBody) {
        Object data = responseBody.getData();
        if (data instanceof List) {
            List<ExaminationSigning> examinationSignings = (List<ExaminationSigning>) data;
            NOW_ITEMS.clear();
            for (ExaminationSigning examinationSigning : examinationSignings) {
                ExaminationSigningPlaceholderContent.addItem(new ExaminationSigningPlaceholderContent.PlaceholderItem(
                        examinationSigning.getIdcard(),
                        examinationSigning.getName(),
                        examinationSigning.getSex(),
                        examinationSigning.getAge(),
                        examinationSigning.getListenername()
                ));
            }
            adapter.notifyDataSetChanged();
        }
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), "搜索体检签约响应成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onSearchESFailure(String errorMessage) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), "搜索体检签约响应失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}